package surveyBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/6/10.
 */
public class SurveyUser implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
//        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //
        String action = request.getParameter("action");

        //获取用户答卷列表
        if (Objects.equals("getAnswerList", action)){
            String date = request.getParameter("date");
            String surveyName = request.getParameter("surveyName").trim();
            surveyName = surveyName.equals("") ? "" : " AND surveyname LIKE '%" + surveyName + "%'";
            String select = "SELECT a.surveyid,b.surveyname,a.username,b.surveycontent,a.contentscore,a.contentaverage,a.usermark";
            String from = " FROM surveyuser a RIGHT JOIN survey b ON a.surveyid = b.id ";
            String where = " WHERE a.updatetime LIKE '" + date + "%' AND a.state=1" + surveyName;
            String orderBy = " ORDER BY updatetime DESC";
            String sql = select + from + where + orderBy;
//            System.out.println(sql);
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            returnJson.setResult(sqlReturnModel.getSelectReturnDataAsJsonArray());

        }

        //获取某个问卷的详细明细，问卷名 问卷题目列表
        if (Objects.equals("getQuestionList", action)){
            String surveyId = request.getParameter("surveyId");
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String sql = "SELECT surveyname, surveycontent FROM survey WHERE id=" + surveyId;
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            String surveyContent = sqlReturnModel.getSelectReturnDataAsJsonArray().getJSONArray(0).getString(1);
            String surveyName = sqlReturnModel.getSelectReturnData().get(0).get(0);
            JSONObject result = new JSONObject();
            result.put("surveyname", surveyName);
            result.put("surveycontent", JSONArray.parseArray(surveyContent));
            returnJson.setResult(result);
        }
        //验证文件是否已经提交
        if (Objects.equals("checkAvailable", action)){
            String uniqueId = request.getParameter("uniqueid");
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String sql = "SELECT state FROM surveyuser WHERE surveyuniqueid='" + uniqueId + "'";
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            String state = sqlReturnModel.getSelectReturnData().get(0).get(0);
            returnJson.setResult(state);
        }


        return returnJson.toString();
    }


}
